package juc.thread.reentrantLock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/*
* ReentrantLock-锁超时
* */
public class Test3 {
    private static ReentrantLock lock=new ReentrantLock();
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            System.out.println("t1尝试获得锁...");
            try {
//                lock.tryLock();
                if(lock.tryLock(10, TimeUnit.SECONDS)) {
                    try {
                        System.out.println("t1获得锁成功...");
                    } finally {
                        lock.unlock();
                    }
                    return;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("t1获得锁失败");
        }, "t1");
        lock.lock();
        System.out.println("main获得锁...");
        t1.start();
        Thread.sleep(1000);
        System.out.println("一秒后main释放锁");
        lock.unlock();
    }
}
